

var count;
$(document).ready(function () {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    var Modal = $(".modal-msg");
    Modal.fadeOut(0);

    var loader = $('.loading');
    loader.fadeOut(0);
    count = 1;

    $('.get-messages-form').submit(function(event){
        event.preventDefault();
        peekMessages();
    });

    // This handles the preventDefault and calls the related function to perform the functionality
    $('.dequeue-message-form').submit(function(event){
        event.preventDefault();
        dequeueMessage();
    });

    //this function preventDefault of delete-queue-form
    $('.delete-queue-form').submit(function(event){
        event.preventDefault();
        deleteQueue();
    });
});


function addField(){
    $('.queue-form').append('<div class="form-group"><div class="row"><div class="col-md-2"><input type="text" name="key-'+count+'" placeholder="Key" class="form-control"></div><div class="col-md-6"><input type="text" name="value-'+count+'" placeholder="Value" class="form-control"></div><div></div>');
    count++;
}

function createQueue(){
    var form = $('.queue-form');
    var queueName = $('.queue-name').val();
    $.ajax({
        method: 'POST',
        url: url + "/queue/create/"+queueName,
        data: form.serialize(),
        success: function (response) {
            var res = JSON.parse(response);
            var Modal = $(".modal-msg");
            if (res.statusCode == 200) {
                Modal.addClass("msg-success")
                Modal.removeClass('text-danger');
                Modal.addClass("text-success")
                Modal.html("Queue has been created successfully").slideDown(500);
                Modal.delay(3000);
                Modal.slideUp(500);
            } else if (res.statusCode == 400) {
                Modal.addClass("msg-error")
                Modal.addClass("text-danger")
                Modal.html(res.errorMessage).slideDown(500);
                Modal.delay(4000);
                Modal.slideUp(500);
            }
        },
    });
}

function peekMessages(){
    var loader = $('.loading');
    loader.fadeIn(300);
    var queueName = $('.queue-name').val();
    var numMessages = $('.messages-num').val();
    $.ajax({
        method: 'GET',
        url: url + "/queue/peek/"+queueName+"/"+numMessages,
        success: function (response) {
            var res = JSON.parse(response);
            var Modal = $(".modal-msg");
            if (res.statusCode == 200) {
                Modal.addClass("msg-success")
                Modal.addClass("text-success")
                loader.fadeOut(0);
                Modal.html("Results successfully retrieved").slideDown(500);
                $('.results').html(res.view);
                Modal.delay(3000);
                Modal.slideUp(500);
            } else if (res.statusCode == 400) {
                Modal.addClass("msg-error")
                Modal.addClass("text-danger")
                loader.fadeOut(0);
                Modal.html(res.errorMessage).slideDown(500);
                Modal.delay(4000);
                Modal.slideUp(500);
            }
        },
    });
}


// This function makes an ajax call to add message to the queue
function addMessage(){
    var loader = $('.loading');
    loader.fadeIn(300);
    $.ajax({
        method: 'POST',
        url: url + "/queue/addmessage",
        data: $('.queue-message-form').serialize(),
        success: function (response) {
            var res = JSON.parse(response);
            var Modal = $(".modal-msg");
            if (res.statusCode == 200) {
                Modal.addClass("msg-success")
                Modal.addClass("text-success")
                loader.fadeOut(0);
                Modal.html("Message has been added successfully.").slideDown(500);
                $('.results').html(res.view);
                Modal.delay(3000);
                Modal.slideUp(500);
            } else if (res.statusCode == 400) {
                Modal.addClass("msg-error")
                Modal.addClass("text-danger")
                loader.fadeOut(0);
                Modal.html(res.errorMessage).slideDown(500);
                Modal.delay(4000);
                Modal.slideUp(500);
            }
        },
    });
}


// This function is used to dequeue a message from the queue
function dequeueMessage(){
    var loader = $('.loading');
    loader.fadeIn(300);
    $.ajax({
        method: 'POST',
        url: url + "/queue/dequeuemessage",
        data: $('.dequeue-message-form').serialize(),
        success: function (response) {
            var res = JSON.parse(response);
            var Modal = $(".modal-msg");
            if (res.statusCode == 200) {
                Modal.addClass("msg-success")
                Modal.addClass("text-success")
                loader.fadeOut(0);
                Modal.html("Message  has been Dequeued").slideDown(500);
                $('.results').html(res.view);
                Modal.delay(3000);
                Modal.slideUp(500);
            } else if (res.statusCode == 400) {
                Modal.addClass("msg-error")
                Modal.addClass("text-danger")
                loader.fadeOut(0);
                Modal.html(res.errorMessage).slideDown(500);
                Modal.delay(4000);
                Modal.slideUp(500);
            }
        },
    });
}


// This function Deletes a Queue
function deleteQueue(){
    var loader = $('.loading');
    loader.fadeIn(300);
    $.ajax({
        method: 'POST',
        url: url + "/queue/deletequeue",
        data: $('.delete-queue-form').serialize(),
        success: function (response) {
            var res = JSON.parse(response);
            var Modal = $(".modal-msg");
            if (res.statusCode == 200) {
                Modal.addClass("msg-success")
                Modal.addClass("text-success")
                loader.fadeOut(0);
                Modal.html("The Queue has been deleted").slideDown(500);
                $('.results').html(res.view);
                Modal.delay(3000);
                Modal.slideUp(500);
            } else if (res.statusCode == 400) {
                Modal.addClass("msg-error")
                Modal.addClass("text-danger")
                loader.fadeOut(0);
                Modal.html(res.errorMessage).slideDown(500);
                Modal.delay(4000);
                Modal.slideUp(500);
            }
        },
    });
}
